package session09;

import ATM.FileOperation;
import ATM.UserInfo;
import session09.compent.FontPanel;
import session09.compent.JButtonPanel;
import session09.compent.PanePanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RegisterPanel extends PanePanel {
    private JButtonPanel clearBtn;
    private JButtonPanel exitBtn;
    private JButtonPanel sureBtn;

    private FontPanel name;
    private FontPanel pwd;
    private JTextField txt1;
    private JPasswordField txt2;

    private FontPanel againPwd;
    private JPasswordField txt3;

    private UserInfo user;

    public RegisterPanel(ATMFrame frame) {
        super(200, 225, 200);

        this.clearBtn = new JButtonPanel("image/buttonClear.jpg", 50, 550);
        this.clearBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RegisterPanel.this.txt1.setText("");
                RegisterPanel.this.txt2.setText("");
                RegisterPanel.this.txt3.setText("");
            }
        });
        this.exitBtn = new JButtonPanel("image/buttonOff.jpg", 442, 600);
        this.exitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Container atm = frame.getContentP();
                CardLayout cardLayout = (CardLayout) atm.getLayout();
                cardLayout.show(atm, "index");
            }
        });
        this.sureBtn = new JButtonPanel("image/buttonSure.jpg", 832, 550);
        this.sureBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Container atm = frame.getContentP();
                CardLayout cardLayout = (CardLayout) atm.getLayout();
                String name = RegisterPanel.this.txt1.getText();
                String pwd = new String(RegisterPanel.this.txt2.getPassword());
                String apwd = new String(RegisterPanel.this.txt3.getPassword());
                if (pwd.equals(apwd) && !name.equals("")) {
                    RegisterPanel.this.user = new UserInfo(name, apwd, 0);
                    frame.getLogin().getAllUsers().put(name, RegisterPanel.this.user);
                    FileOperation.writeFile(frame.getLogin().getAllUsers());
                    JOptionPane.showMessageDialog(frame, "注册成功");
                    cardLayout.show(atm, "login");
                }else {
                    JOptionPane.showMessageDialog(frame, "注册失败");
                }

            }
        });
        this.againPwd = new FontPanel(40, 130, 20, 150, 30);
        this.againPwd.setText("请重新输入密码");
        this.txt3 = new JPasswordField();

        this.getBackLab().add(this.clearBtn);
        this.getBackLab().add(this.exitBtn);
        this.getBackLab().add(this.sureBtn);

        this.name = new FontPanel(40, 30, 20, 150, 30);
        this.name.setText("请输入用户名");
        this.pwd = new FontPanel(40, 80, 20, 150, 30);
        this.pwd.setText("请输入密码");
        this.txt1 = new JTextField();
        this.txt2 = new JPasswordField();

        this.getImgLab().add(this.name);
        this.txt1.setBounds(200, 30, 160, 30);
        this.getImgLab().add(this.txt1);
        this.getImgLab().add(this.pwd);
        this.txt2.setBounds(200, 80, 160, 30);
        this.getImgLab().add(this.txt2);
        this.getImgLab().add(this.againPwd);
        this.txt3.setBounds(200, 130, 160, 30);
        this.getImgLab().add(this.txt3);
    }
}
